package com.dxyusn.schedule.elasticjob.springboot.config;

import javax.sql.DataSource;

import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
import org.apache.shardingsphere.elasticjob.script.props.ScriptJobProperties;
import org.apache.shardingsphere.elasticjob.tracing.api.TracingConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.dxyusn.schedule.elasticjob.springboot.job.MyDataFlowJob;
import com.dxyusn.schedule.elasticjob.springboot.job.MySimpleJob;

@Configuration
public class ElasticJobConfig
{
	@Autowired
	private ZookeeperRegistryCenter regCenter;

	// 定义调度器
	@Bean(name = "simpleJobSchedule", initMethod = "schedule")
	public ScheduleJobBootstrap simpleJobScheduler(TracingConfiguration<DataSource> tracingConfig,
			final MySimpleJob simpleJob, @Value("${job.cron}") final String cron,
			@Value("${job.shardingTotalCount}") final int shardingTotalCount,
			@Value("${job.shardingItemParameters}") final String shardingItemParameters,
			@Value("${job.jobName}") final String jobName)
	{
		// 定义日志数据库事件溯源配置
		JobConfiguration jobConfig = JobConfiguration.newBuilder(jobName, shardingTotalCount).cron(cron)
				.shardingItemParameters(shardingItemParameters)
				//				.jobListenerTypes("simpleJobListener")
				.overwrite(true).failover(true).build();
		jobConfig.getExtraConfigurations().add(tracingConfig);
		return new ScheduleJobBootstrap(regCenter, simpleJob, jobConfig);
	}

	@Bean(name = "dataFlowJobSchedule", initMethod = "schedule")
	public ScheduleJobBootstrap dataFlowJobScheduler(TracingConfiguration<DataSource> tracingConfig,
			final MyDataFlowJob dataFlowJob, @Value("${job.dataflow.cron}") final String cron,
			@Value("${job.dataflow.shardingTotalCount}") final int shardingTotalCount,
			@Value("${job.dataflow.shardingItemParameters}") final String shardingItemParameters,
			@Value("${job.dataflow.jobName}") final String jobName)
	{
		// 创建作业配置
		JobConfiguration jobConfig = JobConfiguration.newBuilder(jobName, shardingTotalCount).cron(cron)
				.shardingItemParameters(shardingItemParameters)
				//				.jobListenerTypes("simpleJobListener")
				.overwrite(true).failover(true).build();
		jobConfig.getExtraConfigurations().add(tracingConfig);
		return new ScheduleJobBootstrap(regCenter, dataFlowJob, jobConfig);
	}

	@Bean(name = "scriptJobSchedule", initMethod = "schedule")
	public ScheduleJobBootstrap scriptJobSchedule(TracingConfiguration<DataSource> tracingConfig,
			@Value("${job.script.cron}") final String cron,
			@Value("${job.script.shardingTotalCount}") final int shardingTotalCount,
			@Value("${job.script.shardingItemParameters}") final String shardingItemParameters,
			@Value("${job.script.jobName}") final String jobName)
	{
		// 创建作业配置
		JobConfiguration jobConfig = JobConfiguration.newBuilder(jobName, shardingTotalCount).cron(cron)
				.shardingItemParameters(shardingItemParameters)
				.setProperty(ScriptJobProperties.SCRIPT_KEY, "/Users/dongxiyan/code/job.sh").overwrite(true)
				.failover(true).build();
		jobConfig.getExtraConfigurations().add(tracingConfig);
		return new ScheduleJobBootstrap(regCenter, "SCRIPT", jobConfig);
	}

}
